package cn.hs.ph.dao.impl;

import cn.hs.ph.dao.ShopDao;
import cn.hs.ph.entity.Shop;
import cn.hs.ph.util.JDBCDruidUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insert(Shop shop) {
        String sql = "insert into shop values(null,now(),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object obj[] = {
               shop.getConcernStatus(),
                shop.getDistance(),
                shop.getSalesVolume(),
                shop.getShopAccount(),
                shop.getShopAddress(),
                shop.getShopCategoryList(),
                shop.getShopCity(),
                shop.getShopCommunitySort(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopGoodsList(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopOrderCount(),
                shop.getShopPassword(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopSort(),
                shop.getShopStatus(),
                shop.getShopUserId(),
                shop.getCommunityId()
        };
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int delete(int shopId) {
        String sql = "delete from shop where shopId=?";
        Object[] obj = new Object[]{shopId};
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public int update(Shop shop) {
        String sql = "update shop set shopAddress=?,shopCity=?,shopDescribe=?,shopDistrict=?,shopImage=?,shopLatitude=?,shopLongitude=?,shopName=?,shopPhone=?,shopProvince=? where shopId=?";
        Object[] obj = new Object[]{
                shop.getShopAddress(),
                shop.getShopCity(),
                shop.getShopDescribe(),
                shop.getShopDistrict(),
                shop.getShopImage(),
                shop.getShopLatitude(),
                shop.getShopLongitude(),
                shop.getShopName(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopId()
        };
        return JDBCDruidUtil.update(sql, obj);
    }

    @Override
    public List<Shop> selectAll(Map<String, String> map) {
        List<Shop> list = new ArrayList<>();
        //分页查询 模糊查询 时间范围查询
        String sql = "select * from shop where 1=1";
        List<Object> params = new ArrayList<>();
        //判断是否需要进行模糊查询,门店名称
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ?";
            params.add("%" + shopName + "%");
        }
        //判断是否需要进行模糊查询,门店手机号
        String shopPhone = map.get("shopPhone");
        if (shopPhone != null && !"".equals(shopPhone)) {
            sql += " and shopPhone like ?";
            params.add("%" + shopPhone + "%");
        }
        //判断是否需要时间范围查询
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            String endTime = map.get("endTime");
            params.add(startTime);
            params.add(endTime);
        }
        sql+=" order by shopId desc";
        //判断是否需要分页查询
        String pn = map.get("pageNum");
        if (pn != null && !"".equals(pn)) {
            sql += " limit ?,?";
            int pageNum = Integer.parseInt(pn);
            int pageSize = Integer.parseInt(map.get("pageSize"));
            params.add((pageNum - 1) * pageSize);
            params.add(pageSize);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                Shop shop = new Shop(
                        res.getInt(1),
                        res.getDate(2),
                        res.getString(3),
                        res.getString(4),
                        res.getInt(5),
                        res.getInt(6),
                        res.getString(7),
                        res.getString(8),
                        res.getString(9),
                        res.getString(10),
                        res.getString(11),
                        res.getString(12),
                        res.getString(13),
                        res.getString(14),
                        res.getString(15),
                        res.getString(16),
                        res.getString(17),
                        res.getInt(18),
                        res.getString(19),
                        res.getString(20),
                        res.getString(21),
                        res.getString(22),
                        res.getInt(23),
                        res.getInt(24),
                        res.getInt(25)

                );
                list.add(shop);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return list;
    }

    @Override
    public int selectCount(Map<String, String> map) {
        int count = 0;
        //分页查询 模糊查询 时间范围查询
        String sql = "select count(1) from shop where 1=1";
        List params = new ArrayList<>();
        //判断是否需要进行模糊查询,小区名称
        String shopName = map.get("shopName");
        if (shopName != null && !"".equals(shopName)) {
            sql += " and shopName like ?";
            params.add("%" + shopName + "%");
        }
        //判断是否需要进行模糊查询,门店手机号
        String shopPhone = map.get("shopPhone");
        if (shopPhone != null && !"".equals(shopPhone)) {
            sql += " and shopPhone like ?";
            params.add("%" + shopPhone + "%");
        }
        //判断是否需要时间范围查询
        String startTime = map.get("startTime");
        if (startTime != null && !"".equals(startTime)) {
            sql += " and addTime between ? and ?";
            String endTime = map.get("endTime");
            params.add(startTime);
            params.add(endTime);
        }
        Object[] obj = params.toArray();
        ResultSet res = JDBCDruidUtil.query(sql, obj);
        while (true) {
            try {
                if (!res.next()) break;
                count = res.getInt(1);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        JDBCDruidUtil.close();
        return count;
    }
}
